package com.sukaiyi.generator.utils;

import com.intellij.icons.AllIcons;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.ide.highlighter.XmlFileType;
import com.intellij.json.JsonFileType;
import com.intellij.openapi.editor.EditorSettings;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.PlainTextFileType;
import com.intellij.openapi.project.Project;
import com.intellij.ui.EditorTextField;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;

public class IdeaUIUtils {
    public static EditorTextField createEditorTextField(Project project, FileType fileType) {
        return new EditorTextField(null, project, fileType, false, false) {
            @Override
            protected boolean shouldHaveBorder() {
                return StringUtils.isNotEmpty(getText());
            }

            @Override
            protected EditorEx createEditor() {
                EditorEx editor = super.createEditor();
                final EditorSettings settings = editor.getSettings();
                settings.setFoldingOutlineShown(true);
                settings.setLineNumbersShown(true);
                settings.setWheelFontChangeEnabled(true);
                editor.setShowPlaceholderWhenFocused(true);
                editor.setVerticalScrollbarVisible(true);
                editor.setHorizontalScrollbarVisible(true);
                editor.setFontSize(14);
                return editor;
            }
        };
    }

    private static Map<String, FileType> fileTypeMap = new HashMap<String, FileType>() {{
        put(".java", JavaFileType.INSTANCE);
        put(".xml", XmlFileType.INSTANCE);
        put(".js", JsonFileType.INSTANCE);
        put(".json", JsonFileType.INSTANCE);
    }};
    private static Map<String, Icon> iconMap = new HashMap<String, Icon>() {{
        put(".java", AllIcons.FileTypes.Java);
        put(".xml", AllIcons.FileTypes.Xml);
        put(".js", AllIcons.FileTypes.JavaScript);
        put(".json", AllIcons.FileTypes.Json);
    }};

    @NotNull
    public static Icon getIconByFileSuffix(@NotNull String suffix){
        return iconMap.getOrDefault(suffix.toLowerCase(), AllIcons.FileTypes.Unknown);
    }

    @NotNull
    public static FileType getFileTypeByFileSuffix(@NotNull String suffix){
        return fileTypeMap.getOrDefault(suffix.toLowerCase(), PlainTextFileType.INSTANCE);
    }
}
